﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExpertMIS.Brokers;
using ExpertMIS.Entities;
using System.Text;

namespace ExpertMIS
{
    public partial class SocialService : System.Web.UI.Page
    {
        private int _hotCount = 7;
        private int _lastCount = 10;
        private int _domainQuestionCount = 6;
        private QuestionBroker _questionBroker = new QuestionBroker();
        private DomainBroker _domainBroker = new DomainBroker();
        private Dictionary<string, string> _dictDomains = new Dictionary<string, string>();
        private DomainEntity _currentDomain = null;

        private void RefreshHotQuestions()
        {
            if (!int.TryParse(ExpertMIS.Codes.Utility.GetEnvironmentValue("hot.count"), out _hotCount))
            {
                _hotCount = 10;
            }
            repHotQuestions.DataSource = _questionBroker.GetHotQuestion(_hotCount);
            repHotQuestions.DataBind();
        }

        private void RefreshNewQuestions()
        {
            if (!int.TryParse(ExpertMIS.Codes.Utility.GetEnvironmentValue("last.count"), out _lastCount))
            {
                _lastCount = 10;
            }
            repNewQuestions.DataSource = _questionBroker.GetLastQuestions(_lastCount);
            repNewQuestions.DataBind();
        }

        private void RefreshDomainQuestions(string domainOID, Repeater repControl)
        {
            if (string.IsNullOrEmpty(domainOID) || repControl == null) return;

            _currentDomain = _domainBroker.GetDomainByOID(domainOID);
            repControl.DataSource = _questionBroker.GetDomainLastQuestion(domainOID, _domainQuestionCount);
            repControl.DataBind();
        }

        private void RefreshAllDomainQuestion()
        {
            foreach (string ctrlId in _dictDomains.Keys)
            {
                RefreshDomainQuestions(_dictDomains[ctrlId], FindControl(ctrlId) as Repeater);
            }
        }

        protected string GetDomainOID(string repeaterCtrlId)
        {
            if (_dictDomains.ContainsKey(repeaterCtrlId))
            {
                return _dictDomains[repeaterCtrlId];
            }
            return "-1";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string domainList = ExpertMIS.Codes.Utility.GetEnvironmentValue("domain.location");
            string[] domainExpressions = domainList.Split(',');

            foreach (string expression in domainExpressions)
            {
                if (expression.IndexOf("=") < 0) continue;
                string ctrlid = expression.Substring(0, expression.IndexOf("=")).Trim();
                string domainOID = expression.Substring(expression.IndexOf("=") + 1).Trim();
                if (!_dictDomains.ContainsKey(ctrlid) && !string.IsNullOrEmpty(domainOID))
                    _dictDomains.Add(ctrlid, domainOID);
            }

            anchorChatRoom.HRef = ExpertMIS.Codes.Utility.GetEnvironmentValue("chatroom");

            RefreshHotQuestions();
            RefreshNewQuestions();
            RefreshAllDomainQuestion();

            ucBulletinList1.ShowTitle = false;
            ucBulletinList1.ShowNewIcon = true;
            ucBulletinList1.BulletinCatalog = ExpertMIS.UserControl.BulletinCatalog.SocailBulletin;
        }

        protected void repDomain1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Header && _currentDomain != null)
            {
                Label lblTitle = e.Item.FindControl("lblTitle") as Label;
                HyperLink hlMore = e.Item.FindControl("hlMore") as HyperLink;

                if (lblTitle != null)
                {
                    lblTitle.Text = _currentDomain.DomainName;
                }

                if (hlMore != null)
                {
                    hlMore.Target = "_self";
                    hlMore.NavigateUrl = string.Format("Forums/DomainIntroduce.aspx?oid={0}", _currentDomain.OID);
                }
            }
        }

        protected void repHotQuestions_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                QuestionEntity question = e.Item.DataItem as QuestionEntity;
                Label lblDomains = e.Item.FindControl("lblDomains") as Label;
                StringBuilder sbDomains = new StringBuilder();
                if (question != null && lblDomains != null)
                {
                    foreach(DomainEntity domain in _domainBroker.GetDomainsByQuerytionOID(question.OID))
                    {
                        sbDomains.Append(string.Format("【<a href='Forums/DomainIntroduce.aspx?oid={0}' target='_self'>{1}</a>】",domain.OID, domain.DomainName));
                    }

                    lblDomains.Text = sbDomains.ToString();
                    if (question.FAQ)
                    {
                        lblDomains.CssClass = "faq";
                    }
                }
            }
        }

        protected void repNewQuestions_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                QuestionEntity question = e.Item.DataItem as QuestionEntity;
                Label lblDomains = e.Item.FindControl("lblDomains") as Label;
                StringBuilder sbDomains = new StringBuilder();
                if (question != null && lblDomains != null)
                {
                    foreach (DomainEntity domain in _domainBroker.GetDomainsByQuerytionOID(question.OID))
                    {
                        sbDomains.Append(string.Format("【<a href='Forums/DomainIntroduce.aspx?oid={0}' target='_self'>{1}</a>】", domain.OID, domain.DomainName));
                    }

                    lblDomains.Text = sbDomains.ToString();
                    if (question.FAQ)
                    {
                        lblDomains.CssClass = "faq";
                    }
                }
            }
        }
    }
}
